﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Library;

namespace Sport_Shop.Winform
{
    public partial class QLDonHang : Form
    {
        public QLDonHang()
        {
            InitializeComponent();
        }

        List<DonDatHang> ListDDH = new List<DonDatHang>();
        List<DonDatHang> DDHtoDelete = new List<DonDatHang>();

        //Check tình trạng giao hàng
        #region Check tình trạng giao hàng
        private void dgvDonHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DonDatHang ddh = (DonDatHang)dgvDonHang.Rows[e.RowIndex].DataBoundItem;

                if (e.ColumnIndex == 6)
                {
                    ddh.updated = true;
                }

                List<ChiTietDDH> ListChiTietDH = new List<ChiTietDDH>();

                ListChiTietDH = ChiTietDDH.GetList(ddh.MaDDH);
                
                dgvChiTietDDH.AutoGenerateColumns = false;
                dgvChiTietDDH.DataSource = null;
                dgvChiTietDDH.DataSource = ListChiTietDH;
            }
        }
        #endregion

        //Button Xóa
        #region Button Xóa
        private void btXoa_Click(object sender, EventArgs e)
        {
            if (dgvDonHang.SelectedRows.Count == 1)
            {
                DonDatHang ddh = (DonDatHang)dgvDonHang.SelectedRows[0].DataBoundItem;

                if (MessageBox.Show("Muốn xóa thiệt hả?", "Xác nhận xóa", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ListDDH.Remove(ddh);
                    DDHtoDelete.Add(ddh);

                    dgvDonHang.DataSource = null;
                    dgvDonHang.DataSource = ListDDH;
                }
            }
        }
        #endregion

        //Button Lưu
        #region Button Lưu
        private void btSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Lưu đó nha!!!", "Xác nhận lưu", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                foreach (DonDatHang d in ListDDH)
                {
                    if (d.updated == true)
                        d.Update();
                }

                //Lưu Delete
                try
                {
                    foreach (DonDatHang p in DDHtoDelete)
                    {
                        //Trả lại số lượng cho kho
                        //Delete ChiTietDDH
                        foreach (ChiTietDDH ct in p.ChiTietDDH)
                        {
                            ChiTietKho.AddKho(ct.ID_SP, ct.SoLuong);

                            ct.Delete();
                        }


                        //Delete DonDatHang
                        p.Delete();

                    }

                    DDHtoDelete = new List<DonDatHang>();
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                MessageBox.Show("Đã lưu thành công!!!");
                ListDDH = DonDatHang.GetList();
                dgvChiTietDDH.DataSource = null;
                dgvDonHang.DataSource = null;
                dgvDonHang.DataSource = ListDDH;
            }
        }
        #endregion

        //Form Load
        #region Form Load
        private void QLDonHang_Load(object sender, EventArgs e)
        {
            //Load gridview
            ListDDH = DonDatHang.GetList();
                
            dgvDonHang.AutoGenerateColumns = false;
            dgvDonHang.DataSource = ListDDH;

        }
        #endregion

        //Lọc theo ngày
        #region Lọc theo ngày
        private void dateTu_ValueChanged(object sender, EventArgs e)
        {
            List<DonDatHang> result = null;

            result = DonDatHang.FilterList(dateTu.Value, dateDen.Value, ListDDH);

            dgvDonHang.AutoGenerateColumns = false;
            dgvDonHang.DataSource = null;
            dgvDonHang.DataSource = result;
        }
        #endregion
    }
}
